<?php

namespace App\Library\Rules;

use Illuminate\Contracts\Validation\Rule;

class SpecsRule implements Rule
{
    private string $errorMessage = '商品规格格式错误';

    /**
     * passes
     * @param string $attribute
     * @param mixed $value
     * @return boolean
     */
    public function passes($attribute, $value): bool
    {
        if (!is_array($value)) return false;
        foreach ($value as $k => $v) {
            if (!is_string($k)) return false;
            else if (strlen($k) !== 20) return false;
            else if (!preg_match(__('com.serialNumberReg'), $k)) return false;
            else if (!cache()->has('shopProductSpec:' . $k)) return false;
            //else if (!is_int($v)) return false;
        }
        return true;
    }

    /**
     * message
     * @return string
     */
    public function message(): string
    {
        return $this->errorMessage;
    }
}
